using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;

namespace Caece.Libreria.Protocolo
{
	public class Util
	{
		public static byte[] StrToByteArray(string str)
		{
			string[] ByteStrings;
			ByteStrings = str.Split(".".ToCharArray());
			byte[] ByteOut;

			ByteOut = new byte[ByteStrings.Length];

			for (int i = 0; i < ByteStrings.Length; i++)
			{
				ByteOut[i] = Convert.ToByte(ByteStrings[i]);
			}

			return ByteOut;
		}

		public static string ByteArrayToStr(byte[] param)
		{
			string salida = String.Empty;
			for (int i = 0; i < param.Length; i++)
			{
				salida = salida + param[i].ToString() + ".";
			}

			salida = salida.Substring(0, salida.Length - 1);

			return salida;

			//System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
			//return encoding.GetString(param);
		}

		public static T GetConfig<T>(string key)
		{
			string value = ConfigurationManager.AppSettings[key];
			return (T)Convert.ChangeType(value, typeof(T));
		}
     
		private static string GetTempPath()
		{
			string path = System.Environment.GetEnvironmentVariable("TEMP");
			if (!path.EndsWith("\\")) path += "\\";
			path = "";
			return path;
		}

		private static void LogMessageToFile(string msg, int id)
		{
			System.IO.StreamWriter sw = System.IO.File.AppendText(
				GetTempPath() + "My Log File" + id.ToString() + ".txt");
			try
			{
				string logLine = System.String.Format(
					"{0:G}: {1}.", System.DateTime.Now, msg);
				sw.WriteLine(logLine);
			}
			finally
			{
				sw.Close();
			}
		}

		public static void Log(string message)
		{
			Console.WriteLine(message);
		}

		public static void LogFormat(string message, object arg0)
		{
			Console.WriteLine(String.Format(message, arg0));
		}

		public static void LogFormat(string message, object arg0, object arg1)
		{
			Console.WriteLine(String.Format(message, arg0, arg1));
		}

		public static void LogFormat(string message, object arg0, object arg1, object arg2)
		{
			Console.WriteLine(String.Format(message, arg0, arg1, arg2));
		}
	}

	
}
